/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.modules.shop.rest;

import java.util.Arrays;

import cn.hutool.crypto.SecureUtil;
import co.yixiang.dozer.service.IGenerator;
import lombok.AllArgsConstructor;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.shop.domain.ExpressArea;
import co.yixiang.modules.shop.service.ExpressAreaService;
import co.yixiang.modules.shop.service.dto.ExpressAreaQueryCriteria;
import co.yixiang.modules.shop.service.dto.ExpressAreaDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author chajiu
 * @date 2021-04-25
 */
@AllArgsConstructor
@Api(tags = "方案区域管理")
@RestController
@RequestMapping("/api/ExpressArea")
public class ExpressAreaController {

    private final ExpressAreaService ExpressAreaService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','ExpressArea:list')")
    public void download(HttpServletResponse response, ExpressAreaQueryCriteria criteria) throws IOException {
        ExpressAreaService.download(generator.convert(ExpressAreaService.queryAll(criteria), ExpressAreaDto.class), response);
    }

    @GetMapping
    @Log("查询方案区域")
    @ApiOperation("查询方案区域")
    @PreAuthorize("@el.check('admin','ExpressArea:list')")
    public ResponseEntity<Object> getExpressAreas(ExpressAreaQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(ExpressAreaService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增方案区域")
    @ApiOperation("新增方案区域")
    @PreAuthorize("@el.check('admin','ExpressArea:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ExpressArea resources) {
        return new ResponseEntity<>(resources, HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改方案区域")
    @ApiOperation("修改方案区域")
    @PreAuthorize("@el.check('admin','ExpressArea:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ExpressArea resources) {
        ExpressAreaService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除方案区域")
    @ApiOperation("删除方案区域")
    @PreAuthorize("@el.check('admin','ExpressArea:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        Arrays.asList(ids).forEach(id -> {
            ExpressAreaService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
